package com.lt.flashcard.view;

import java.text.DecimalFormat;
import java.util.List;

import org.zkoss.lang.OperationException;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;

/**
 * Render the wordlist detail page
 * @author ocsen
 * @version 1.0
 */
public class WordlistDetailCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	/**
	 * Store current flashcard set
	 */
	private FlashcardSet flashcardset = null;

	@Wire
	Include list_flashcard;
	@Wire
	Label sotu;
	@Wire
	Textbox rating;
	@Wire
	Label sao_diem;
	@Wire
	Label voteResult;
	@Wire
	Label voteTotal;
	
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		super.doBeforeComposeChildren(comp);
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		String strId = Executions.getCurrent().getParameter("id");
		try {
			int id = Integer.parseInt(strId);
			if(id > 0) {
				flashcardset = srv.findByID(new Long(id));
			}
		}
		catch (Exception e) { }
		
		if(flashcardset == null) {
			throw new OperationException("Unknow flashcardset");
		}
		srv.updateViewCount(flashcardset);
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		String so = String.valueOf(flashcardset.getFlashcards().size());
		sotu.setValue(so);
	};
	
	public FlashcardSet getFlashcardset() {
		return flashcardset;
	}

	public void setFlashcardset(FlashcardSet flashcardset) {
		this.flashcardset = flashcardset;
	}

	public List<FlashcardSet> getRelatedList() {
		FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		return srv.getRelatedFlashcardSet(flashcardset);
	}
		
	 public List<FlashcardSet> getOriginalFlashcardSet() {
		 FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
		 return srv.getOriginalFlashcardSet(flashcardset);
	 }
	 
	 @Listen("onClick=#save_set_top")
	 public void saveSet(){
		 FlashcardSet newSet = new FlashcardSet(flashcardset);
		 newSet.setAuthor(Utility.getUser());
		 FlashcardSetService fsSrv = Repository.getInstance().getFlashcardSetService();
		 fsSrv.saveFlashcardsIntoNewFlashcardset(newSet, flashcardset);
		 fsSrv.save(newSet);
		 fsSrv.resetOriginalFlashcardSet(newSet);
		 Executions.sendRedirect("flashcard.zul?task=edit&id="+newSet.getId().toString());
	 }
	 
	 @Listen("onChange=#rating")
	 public void saveRating(){
		 FlashcardSetService fsrv = Repository.getInstance().getFlashcardSetService();
		 fsrv.updateRating(flashcardset, new Integer(rating.getValue()));
		 DecimalFormat df = new DecimalFormat("#.##");
		 String voteResult = df.format(flashcardset.getVoteResult());
		 this.sao_diem.setValue(voteResult);
		 this.voteResult.setValue(voteResult);
		 this.voteTotal.setValue(String.valueOf(flashcardset.getVoteTotal()));
		 Clients.evalJavaScript("showNotice('" + Labels.getLabel("wordlist_detail.rating.updatesuccess") + "')");
	 }
}
